package com.example.covid19.FaceUtil;

import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Studentinfo;
import com.example.covid19.mapper.StudentinfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import static com.example.covid19.FaceUtil.Faceset.getBytesFromFile;
import static com.example.covid19.FaceUtil.Faceset.post;

@Slf4j
@RestController
@RequestMapping("/Compare")
@Component
public class CompareApi {

    private final StudentinfoMapper studentinfoMapper;

    public CompareApi(StudentinfoMapper studentinfoMapper) {
        this.studentinfoMapper = studentinfoMapper;
    }


    @RequestMapping("/com")
    public Result face(@RequestPart("file") MultipartFile multipartFile) {
        List<Studentinfo> studentinfos = studentinfoMapper.studentinfs();
        String str2;
        str2 = upLoad(multipartFile);
        File file1 = new File (str2);
        File file2 = new File("D:\\test\\1.png");
        byte[] buff1 = getBytesFromFile(file1);
        byte[] buff2 = getBytesFromFile(file2);
        String url = "https://api-cn.faceplusplus.com/facepp/v3/compare";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "puaPEqTKRAATMQu35U8y3SwQGiLJduX0");
        map.put("api_secret", "vXTB_SEI0P3sZg3UPcJym7bxb6MrAW8p");
        map.put("return_landmark", "1");
        map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        byteMap.put("image_file1", buff1);
        byteMap.put("image_file2", buff2);
        try{
            byte[] bacd = post(url, map, byteMap);
            String str = new String(bacd);
            System.out.println(str);
            return Result.success(200,"ok", StringEscapeUtils.unescapeJava(StringEscapeUtils.unescapeJava(str)));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(200,"ok",null);
    }
    @RequestMapping("/test")
    public Result test(){
        return Result.success(300,"成功",studentinfoMapper.studentinfs());
    }
    /**
     * MultipartFile 转换为 File 文件
     *
     * @param
     * @return
     */
    @RequestMapping("/upload")
    public String upLoad(@RequestPart("file") MultipartFile multipartFile){
        log.info("文件上传开始");
        log.info("文件{}",multipartFile.getOriginalFilename());
        String str;
        if (!multipartFile.isEmpty()){
            try {
                //上传的文件需要保存的路径和文件名称，路径需要存在，否则报错
                multipartFile.transferTo(new File("D:/"+multipartFile.getOriginalFilename()));
                str="D:/"+multipartFile.getOriginalFilename();

            } catch (IllegalStateException | IOException e){
                e.printStackTrace();
                return "上传失败";
            }
        } else {
            return "请上传文件";
        }
        return str;
    }


}
